package cn.lucky.springcloud.controller;

import cn.lucky.springcloud.entities.CommonResult;
import cn.lucky.springcloud.entities.Payment;
import cn.lucky.springcloud.service.ConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author lingpeng.jiang
 * @create 2021-01-20-21:36
 */
@RestController
@Slf4j
@RefreshScope
public class ConfigClientController {

    @Resource
    private ConfigClientService configClientService;

    @Value("${config.version}")
    private String version;


    @GetMapping("/config/version")
    public String getVersion(){
        return version;
    }

    @GetMapping("/config/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = configClientService.getPaymentById(id);
        if (payment != null){
            log.info("查询成功");
            return new CommonResult<>(200,"查询成功,version:" + version,payment);
        } else {
            log.info("查询失败");
            return new CommonResult<>(444,"查询失败,ID为" + id,null);
        }
    }
}
